//
//  ShareBoard.m
//  Anyair
//
//  Created by karlcool on 14-5-21.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "ShareBoard.h"

@implementation ShareBoard

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)layoutSubviews {
    
    if (!aniFlag) {
        NSArray *imageArray = @[[UIImage imageNamed:@"Share01"],[UIImage imageNamed:@"Share02"],[UIImage imageNamed:@"Share03"],[UIImage imageNamed:@"Share04"],[UIImage imageNamed:@"Share05"],[UIImage imageNamed:@"Share06"],[UIImage imageNamed:@"Share07"],[UIImage imageNamed:@"Share08"]];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        NSLog(@"!!");
        UIView *board = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 230)];
        board.center = self.center;
        board.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:227.0/255.0 blue:227.0/255.0 alpha:1];
        board.layer.cornerRadius = 5;
        [self addSubview:board];
        
        for (int i=0; i<8; i++) {
            if (i<4) {
                UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(70*i+8, 10, 55, 75)];
                UIImage *shareImage = imageArray[i];
                shareBtn.tag = i;
                [shareBtn setImage:shareImage forState:UIControlStateNormal];
                [shareBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
                [board addSubview:shareBtn];
            } else {
                int j = i - 4;
                UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(70*j+8, 95, 55, 75)];
                UIImage *shareImage = imageArray[i];
                shareBtn.tag = i;
                [shareBtn setImage:shareImage forState:UIControlStateNormal];
                [shareBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
                [board addSubview:shareBtn];
            }
        }
        
        UIView *breakLine = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 280, 1)];
        breakLine.backgroundColor = [UIColor lightGrayColor];
        [board addSubview:breakLine];
        
        UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 182, 280, 54)];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.tag = 8;
        cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [cancelBtn setTitleColor:[UIColor colorWithRed:0 green:0.478 blue:1 alpha:1] forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [board addSubview:cancelBtn];
        self.alpha = 0;
        [self popAnimate:board];
        aniFlag = YES;
    }

    
    
    
}

- (void)clickBtn:(UIButton*)sender {
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(clickShareBtn:)]) {
        [self.delegate clickShareBtn:sender];
    }
}

- (void)popAnimate:(UIView*)aniView{
    
    [UIView animateWithDuration:0.1 animations:
     ^(void){
         self.alpha = 1;
         aniView.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8, 0.8);
     } completion:^(BOOL finished){
         [UIView animateWithDuration:0.2 animations:
          ^(void){
              aniView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.2, 1.2);
          } completion:^(BOOL finished){
              [UIView animateWithDuration:0.1 animations:
               ^(void){
                   aniView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
               } completion:^(BOOL finished){
               }];
          }];
     }];
}
@end
